﻿using B.S.DataBase.MyProject.Domain.Materials;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using System;
using T.E.ERP.Read.API.Applicantions.Command.MaterialsCommand.QueryMaterialsCommand;
using T.E.ERP.Read.API.Dtos.Dtos;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.MaterialsCommandCommand.QueryMaterialsCommandHandler
{
    /// <summary>
    /// 物料基本档查询处理方法
    /// </summary>
    public class Material_BasicQueryCommandHandler:IRequestHandler<Material_BasicQueryCommand,APIResult<List<Material_BasicDtos>>>
    {
        /// <summary>
        /// 物料基本档仓储接口
        /// </summary>
        private readonly IBaseRepository<Material_BasicModel> mRepo;
        /// <summary>
        /// 供应方式
        /// </summary>
        private readonly IBaseRepository<SupplyMethodModel> sRepo;
        /// <summary>
        /// 计量单位
        /// </summary>
        private readonly IBaseRepository<UnitOfMeasurementModel> uRepo;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="mRepo"></param>
        public Material_BasicQueryCommandHandler(IBaseRepository<Material_BasicModel>mRepo,IBaseRepository<SupplyMethodModel>sRepo,IBaseRepository<UnitOfMeasurementModel>uRepo)
        {
            this.mRepo = mRepo;
            this.sRepo = sRepo;
            this.uRepo = uRepo;
        }
        /// <summary>
        /// 处理方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<APIResult<List<Material_BasicDtos>>> Handle(Material_BasicQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<Material_BasicDtos>> result = new APIResult<List<Material_BasicDtos>>();
            var list = from a in mRepo.GetAll().Result
                       join b in sRepo.GetAll().Result on a.supply_method_id equals b.supply_method_id
                       join c in uRepo.GetAll().Result on a.unit_of_measurement_id equals c.unit_of_measurement_id
                       select new Material_BasicDtos
                       {
                           material_id = a.material_id,
                           category_id = a.category_id,
                           material_code = a.material_code,
                           material_name = a.material_name,
                           model_specification = a.model_specification,
                           drawing_number = a.drawing_number,
                           specification_desc = a.specification_desc,
                           unit_of_measurement_id = a.unit_of_measurement_id,
                           supply_method_id = a.supply_method_id,
                           allow_negative_stock= a.allow_negative_stock,
                           keep_material_name = a.keep_material_name,
                           UpdateTime = a.UpdateTime,
                           IsDel = a.IsDel,
                           supply_method_name = b.supply_method_name,
                           unit_of_measurement_name=c.unit_of_measurement_name,
                           orderNumber    =a.orderNumber      , 
                            warehouse     =a.warehouse        ,
                            receiptNumber =a.receiptNumber    ,
                            materialCode  =a.materialCode     ,
                            materialName  =a.materialName     ,
                            specification =a.specification    ,
                            unit          =a.unit             ,
                            pcount        =a.pcount           ,
                            price         =a.price             ,
                       };
            if (request.material_code!=null)
            {
                list = list.Where(x => x.material_code==(request.material_code));
            }
            if (!string.IsNullOrEmpty(request.material_name))
            {
                list = list.Where(x => x.material_name.Contains(request.material_name));
            }
            list = list.OrderByDescending(x => x.material_id);
            result.data = list.ToList();
            result.Msg = APIEnum.查询成功.ToString();
            result.Code = APIEnum.成功;
            result.Count = list.Count();
            return Task.FromResult(result);
        }




    }


}
